package com.lostandfound.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lostandfound.dao.LoggerDao;
import com.lostandfound.entity.MyLogger;

@Repository("loggerDao")
public class LoggerDaoImpl implements LoggerDao {
	
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public Serializable save(MyLogger obj) {
		return null;
	}

	@Override
	public void update(MyLogger obj) {
	}

	@Override
	public void delete(MyLogger obj) {
	}

	@Override
	public MyLogger selectByPrimary(Long id) {
		return null;
	}

	@Override
	public List<?> listLoggerByPage(Map<String, Object> pagemap) {
		List<?> list = null;
		Session session = sessionFactory.getCurrentSession();
		String hql = "from MyLogger ";
		if(pagemap != null){
			if(pagemap.get("page") != null && !((String)pagemap.get("page")).equals("")
					 && pagemap.get("size") != null && !((String)pagemap.get("size")).equals("")){
				int page = Integer.parseInt((String)pagemap.get("page"));
				int size = Integer.parseInt((String)pagemap.get("size"));
				Query query = session.createQuery(hql)
						.setFirstResult((page-1)*size)
						.setMaxResults(size);
				list = query.list();
			}
		}
		return list;
	}

	@Override
	public int getCount() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count(*) from MyLogger ");
		return ((Number) query.iterate().next())
	  	         .intValue();
	}

}
